<template>
  <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
    <template #form-by="{ model, field }">
      <Input v-model:value="model['what']" placeholder="请输入" allowClear>
        <template #addonBefore>
          <Select
            :options="byOptions"
            v-model:value="model[field]"
            style="width: 100px"
            placeholder="请选择"
            allowClear
          />
        </template>
      </Input>
    </template>

    <template #level="{ text }">
      <span title="普通" v-if="text == 0"><Icon icon="clarity:control-lun-line" /></span>
      <span title="一般" v-if="text == 1" style="color: orange"
        ><Icon icon="clarity:control-lun-outline-badged"
      /></span>
      <span title="紧急" v-if="text == 2" style="color: red"
        ><Icon icon="clarity:control-lun-outline-badged"
      /></span>
    </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'clarity:info-standard-line',
            title: '查看',
            onClick: handleView.bind(null, record),
          },
        ]"
      />
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, PropType } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { columns, searchFormSchema, byOptions } from './proxyList.data';
  import { Input, Select } from 'ant-design-vue';
  import { useGo } from '/@/hooks/web/usePage';
  import { Icon } from '/@/components/Icon';
  export default defineComponent({
    name: 'SetupProxy',
    components: { BasicTable, TableAction, Input, Select, Icon },
    props: {
      title: {
        type: String,
        default: '',
      },
      api: {
        type: Function as PropType<() => Promise<void>>,
        required: true,
      },
      type: {
        type: String,
        default: 'my',
      },
    },
    setup(props) {
      const [registerDrawer, { openDrawer }] = useDrawer();

      const [registerTable, { reload }] = useTable({
        title: props.title,
        api: props.api,
        columns: columns.map((item) => {
          if (item.dataIndex === 'realName') {
            if (props.type == 'my') {
              item.title = '原处理者';
            } else {
              item.title = '处理者';
            }
          }
          return item;
        }),
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }
      const go = useGo();
      function handleView(record: Recordable) {
        go({
          path: '/processShow',
          query: {
            flowId: record.id,
            title: record.title,
            isFromProcess: true,
            cacheName: `processShow${record.flowId}`,
          },
        });
      }

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleView,
        handleSuccess,
        byOptions,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
      };
    },
  });
</script>
